added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CppWin7Direct2D / CppWin7Direct2D.cpp
blobac701e4883285ef5696942df6f04af63bbfbb7be
1 /****************************** Module Header ******************************\
2 * Module Name: CppWin7Direct2D.cpp
3 * Project: CppWin7Direct2D
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Defines the entry point for the application.
7 *
8 * This source is subject to the Microsoft Public License.
9 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 * All other rights reserved.
12 * History:
13 * * 10/11/2009 14:54 Yilun Luo Created
14 \***************************************************************************/
16 #include "stdafx.h"
17 #include "CppWin7Direct2D.h"
18 #include "Renderer.h"
20 #define MAX_LOADSTRING 100
22 // Global Variables:
23 HINSTANCE hInst; // current instance
24 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
25 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
27 // Forward declarations of functions included in this code module:
28 ATOM MyRegisterClass(HINSTANCE hInstance);
29 BOOL InitInstance(HINSTANCE, int);
30 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
31 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
33 //The Renderer object.
34 Renderer* pRenderer;
36 int APIENTRY _tWinMain(HINSTANCE hInstance,
37 HINSTANCE hPrevInstance,
38 LPTSTR lpCmdLine,
39 int nCmdShow)
41 UNREFERENCED_PARAMETER(hPrevInstance);
42 UNREFERENCED_PARAMETER(lpCmdLine);
44 // TODO: Place code here.
45 MSG msg;
46 HACCEL hAccelTable;
48 // Initialize global strings
49 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
50 LoadString(hInstance, IDC_CPPWIN7DIRECT2D, szWindowClass, MAX_LOADSTRING);
51 MyRegisterClass(hInstance);
53 // Perform application initialization:
54 if (!InitInstance (hInstance, nCmdShow))
56 return FALSE;
59 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CPPWIN7DIRECT2D));
61 // Main message loop:
62 while (GetMessage(&msg, NULL, 0, 0))
64 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
66 TranslateMessage(&msg);
67 DispatchMessage(&msg);
71 return (int) msg.wParam;
77 // FUNCTION: MyRegisterClass()
79 // PURPOSE: Registers the window class.
81 // COMMENTS:
83 // This function and its usage are only necessary if you want this code
84 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
85 // function that was added to Windows 95. It is important to call this function
86 // so that the application will get 'well formed' small icons associated
87 // with it.
89 ATOM MyRegisterClass(HINSTANCE hInstance)
91 WNDCLASSEX wcex;
93 wcex.cbSize = sizeof(WNDCLASSEX);
95 wcex.style = CS_HREDRAW | CS_VREDRAW;
96 wcex.lpfnWndProc = WndProc;
97 wcex.cbClsExtra = 0;
98 wcex.cbWndExtra = 0;
99 wcex.hInstance = hInstance;
100 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CPPWIN7DIRECT2D));
101 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
102 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
103 wcex.lpszMenuName = NULL;
104 wcex.lpszClassName = szWindowClass;
105 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
107 return RegisterClassEx(&wcex);
111 // FUNCTION: InitInstance(HINSTANCE, int)
113 // PURPOSE: Saves instance handle and creates main window
115 // COMMENTS:
117 // In this function, we save the instance handle in a global variable and
118 // create and display the main program window.
120 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
122 HWND hWnd;
124 hInst = hInstance; // Store instance handle in our global variable
126 hWnd = CreateWindow(szWindowClass, szTitle,
127 WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
128 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
130 if (!hWnd)
132 return FALSE;
135 //Create the Renderer object.
136 pRenderer = new Renderer(hWnd, 800, 600);
138 ShowWindow(hWnd, nCmdShow);
139 UpdateWindow(hWnd);
141 return TRUE;
145 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
147 // PURPOSE: Processes messages for the main window.
149 // WM_COMMAND - process the application menu
150 // WM_PAINT - Paint the main window
151 // WM_DESTROY - post a quit message and return
154 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
156 int wmId, wmEvent;
157 PAINTSTRUCT ps;
158 HDC hdc;
160 switch (message)
162 case WM_PAINT:
163 case WM_DISPLAYCHANGE:
164 hdc = BeginPaint(hWnd, &ps);
165 // TODO: Add any drawing code here...
166 pRenderer->Render();
167 EndPaint(hWnd, &ps);
168 break; case WM_DESTROY:
169 PostQuitMessage(0);
170 break;
171 case WM_LBUTTONDOWN:
172 if(!pRenderer->m_animate)
174 pRenderer->m_animate = true;
175 pRenderer->m_clickedPointX = LOWORD(lParam);
176 pRenderer->m_clickedPointY = HIWORD(lParam);
177 InvalidateRect(pRenderer->m_hwnd, NULL, FALSE);
179 break;
180 default:
181 return DefWindowProc(hWnd, message, wParam, lParam);
183 return 0;